class Media < ActiveRecord::Base
 include FileColumnHelper
 include ActionView::Helpers::AssetTagHelper
 
 belongs_to :artist
 belongs_to :artwork
 belongs_to :region
    
 validates_presence_of :file
 validates_presence_of :media_type
 validates_presence_of :caption
 
 TYPE_PICTURE = 1
 TYPE_AUDIO = 2
 TYPE_VIDEO = 3
 
 OPTIONS = [[Media.l_attr('picture'), Media::TYPE_PICTURE],
            [Media.l_attr('audio'), Media::TYPE_AUDIO],
            [Media.l_attr('video'), Media::TYPE_VIDEO]]
                      

def generate_ordered_media_by_artist
    @pictures = Picture.find_all_by_artist_id(self.artist_id)
    @audios = Audio.find_all_by_artist_id(self.artist_id)
    @videos = Video.find_all_by_artist_id(self.artist_id)

    (@audios + @pictures + @videos)
end

def generate_ordered_media_by_artwork
    @pictures = Picture.find_all_by_artwork_id(self.artwork_id)
    @audios = Audio.find_all_by_artwork_id(self.artwork_id)
    @videos = Video.find_all_by_artwork_id(self.artwork_id)

    (@audios + @pictures + @videos)
end


def generate_html_thumb      
    image_tag url_for_file_column(self, "file", "thumb")
end

  def media_type_number
    read_attribute(:media_type)
  end
  
  def media_type
    case read_attribute(:media_type)
      when TYPE_PICTURE
        Media.l_attr('picture')
      when TYPE_AUDIO
        Media.l_attr('audio')
      when TYPE_VIDEO
        Media.l_attr('video')
    end
  end
end
